//
//  LZMainTabBar.swift
//  Weibo
//
//  Created by lzing on 16/3/19.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit

class LZMainTabBar: UITabBar {
    
    // 定义闭包,存放发送微博回调
    var composeButtonDidClickCallBack:(() -> ())?

    // 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        // 子控件属性
        var index = 0
        let width = UIScreen.mainScreen().bounds.width / CGFloat(5)
        let height = self.bounds.height
        // 遍历子控件
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                // 设置frame
                subview.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: height)
                // 当index为1的时候再加1
                index += index == 1 ? 2 : 1
            }
        }
        // 添加按钮
        addButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: height)
    }
    
    private lazy var addButton : UIButton = {
        let button = UIButton()
        // 设置背景图
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        // 添加点击事件
        button.addTarget(self, action: "composeButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        // 将按钮添加到view上
        self.addSubview(button)
        
        return button
    }()
    
    //MARK: - 按钮点击事件
    @objc private func composeButtonDidClick() {
        composeButtonDidClickCallBack?()
    }
}
